<script type="text/javascript" src="../jquery/js/jquery-1.8.3.js" ></script>
<script type="text/javascript" src="../jquery/js/jquery-ui.js" ></script>
<link rel="stylesheet" href="../jquery/css/jquery-ui.css" />
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.0/themes/base/jquery-ui.css" />

 <script>
	$(function() {
		$( ".datepicker" ).datepicker({ dateFormat: "yy-mm-dd" });
	});
</script>

<?php 
	require_once(dirname(__FILE__)."/../core/safeParam.php");
?>

<div class="tituloOpcionesAdmin">
	<img src="images/icons/report.png"/>
	Reportes
</div>

<form action="index.php?edit=reports" method="GET">
	<input type="hidden" name="edit" value="reports" />
	
	<div class="contenedorReportesTodos">		
		<table>
			<tr>
				<td>
					Desde
				</td>
					
				<td>
					<input type="text" name="s"  class="datepicker" value="<?php echo $start; ?>"/>
				</td>
			</tr>
				
			<tr>
				<td>
					Hasta
				</td>
					
				<td>
					<input type="text" name="e"  class="datepicker"  value="<?php echo $end; ?>"/>
				</td>
			</tr>
			
			<tr>
				<td>
					Comisión:
				</td>
				
				<td>
					<input type="text" name="porcentaje" class="variableComision" value="7" maxlength="3" /> %
				</td>
			</tr>		
		</table>
	</div>
	
	<div class="botonConfigMain">
		<input type="submit" value="Mostrar" />
	</div>
</form>
 			
<?php	
	if(isset($_GET['s']) && isset($_GET['s'])){
		$star			= $_GET['s'];
		$end			= $_GET['e'];
		$porcentaje	= $_GET['porcentaje'];
		
		
		$status_info = $database->select("SELECT * FROM order_status WHERE rid = 1");	
		$orders_list = $database->select("SELECT * FROM order_sum WHERE rid = '". ARID ."' AND DATE BETWEEN '". $start ."' AND '". $end ."' 
													ORDER BY date DESC");
																	
		for($i=0; $i< count($status_info); $i++){
			$total[$i] = $database->select("SELECT SUM(total) AS total
													FROM (SELECT * FROM order_sum WHERE rid='". ARID ."' 
													AND DATE BETWEEN '". $start ."' AND '". $end ."'
													AND status=". $status_info[$i]['id'] ." )r1 ");
		}														
		
		$reports = '<div class="contenedorReportesTodos">		
		<h2>Pedidos entre: <b>'. date('m/d/Y', strtotime($start)) .'</b> y <b>'. date('m/d/Y', strtotime($end)) .'</b></h2>';
			
		if($orders_list) {
			
			$reports .= '<table>
			<tr>
				<td>
					<b>Id</b>
				</td>
					
				<td>
					<b>Estado</b>
				</td>
					
				<td>
					<b>Fecha</b>
				</td>

				<td>
					<b>Total</b>
				</td>										
			</tr>';
			
			for($i=0; $i < count($orders_list); $i++){	
				$estado = $database->select("SELECT * FROM order_status WHERE id='". $orders_list[$i]['status'] ."' ");			
								
				$reports .= '<tr>
					<td>
						'. $orders_list[$i]['id'] .'
					</td>
				
        			<td>
        				'. $estado[0]['name'] .'
        			</td>
        		
        			<td>
						'. $orders_list[$i]['date'] .'        			
        			</td>
		 		
		  			<td>
			  			'. price_format($orders_list[$i]['total']) .'
		  			</td>
      		</tr>
      	
      		<tr>
	      		<td>
      				<a href="javascript:toggle(\'#'. $orders_list[$i]['id'] .' \')" />
	      				Ver descripción
      				</a>
      			</td>
      		</tr>
      	
      		<tr>
	      		<td colspan="4">
      				<div class="reportesDescripcion" id="'. $orders_list[$i]['id'] .'" >
	      				'. $orders_list[$i]['contents'] .'
      				</div>
      			</td>
      		</tr>
      		';	
			}
			
			$reports .= '</table></div>';
			$reports .= '<div class="totalPedidos">
			<table>';
			
			for($i=0; $i< count($status_info); $i++){
				$comision = $total[$i][0][0] * $porcentaje / 100;
				
				$reports .= '<tr><td>Total Pedidos <b><i>'. $status_info[$i]['name'] .'</i></b>: </td>';
				$reports .= '<td>'. $total[$i][0][0] .'$</td>';
				$reports .= '<td>--- Comisión (<b>'. $porcentaje .'</b>%):</td>';
				$reports .= '<td>'. $comision .' $</td></tr>';	
			}	
			
			$reports .='</table></div>';
					
		} else {
			$reports .= '<div class="errorClasico">No hubo pedidos</div>';
		}	

		
		echo $reports;
	}
?>


<script>
	function mostrarDescripcion(div){
		$( div ).toggle();
	}
</script>